package controllers

import (
	"NewEdu/models"
	"NewEdu/utils"
	"encoding/json"
	"fmt"
	"strconv"
	"strings"
	"time"

	beego "github.com/beego/beego/v2/server/web"
)

// 用户的成就
type TAchievmentController struct {
	beego.Controller
}

// URLMapping ...

// Post ...
// @Title Post
// @Description create TAchievment
// @Param	body		body 	models.TAchievment	true		"body for TAchievment content"
// @Param	sn		path 	string	true		"学号"
// @Success 201 {int} models.TAchievment
// @Failure 403 body is empty
// @router /addAchievments/:sn [post]
func (c *TAchievmentController) Post() {
	var v models.TAchievment
	v.UserSN = c.Ctx.Input.Param(":sn")

	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
		if len(v.ImgData) > 0 {
			tempurl := fmt.Sprintf("/upload/user/%s/achievment/", v.UserSN)
			tempfilename := fmt.Sprintf("%d", time.Now().Unix())
			imgPath, err := utils.UploadBase64(v.ImgData, ".."+tempurl, tempfilename)
			if err != nil {
				c.Data["json"] = utils.Export(1001, " DecodeString achievment 失败", err.Error())
				c.ServeJSON()
				return
			}
			v.ImgData = ""
			// 从imgPath中提取文件名部分，构建正确的URL路径
			if strings.HasPrefix(imgPath, "..") {
				v.Img = imgPath[2:] // 去掉前缀的".."
			} else if strings.HasPrefix(imgPath, utils.UploadDir+"..") {
				v.Img = imgPath[len(utils.UploadDir)+2:] // 去掉UploadDir和前缀的".."
			} else {
				v.Img = imgPath // 保持原样
			}
		}
		v.Creattime = time.Now()
		if _, err := models.AddTAchievment(&v); err == nil {
			c.Ctx.Output.SetStatus(201)
			c.Data["json"] = utils.Export(1000, "添加", &v)
		} else {
			c.Data["json"] = utils.Export(1001, "失败", err.Error())
		}
	} else {
		c.Data["json"] = utils.Export(1001, "失败", err.Error())
	}
	c.ServeJSON()
}

// GetOne ...
// @Title Get One
// @Description get TAchievment by id
// @Param	id		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.TAchievment
// @Failure 403 :id is empty
// @router /:id [get]
func (c *TAchievmentController) GetOne() {
	idStr := c.Ctx.Input.Param(":id")
	id, _ := strconv.Atoi(idStr)
	v, err := models.GetTAchievmentById(id)
	if err != nil {
		c.Data["json"] = utils.Export(1001, "查询失败", err.Error())
	} else {
		c.Data["json"] = utils.Export(1000, "查询失败", &v)
	}
	c.ServeJSON()
}

// GetAll ...
// @Title Get All
// @Description get TAchievment
// @Param	Authorization	header	string	true	" Token"
// @Param	user_sn	query	string	false	"学号"
// @Success 200 {object} models.TAchievment
// @Failure 403
// @router /getAchievments [get]
func (c *TAchievmentController) GetAchievments() {
	var user_sn string
	// 从请求中获取 fromSn 参数，并添加到查询参数字典中
	if fromSn := c.GetString("user_sn"); fromSn != "" {
		user_sn = fromSn
	}

	// 调用模型层方法查询商品信息
	goods, err := models.GetTAchievmentBySn(user_sn)
	if err != nil {
		// 查询失败时，返回错误信息
		c.Data["json"] = utils.Export(1001, "查询失败", err.Error())
	} else {
		// 查询成功时，返回商品信息
		c.Data["json"] = utils.Export(1000, "查询成功", goods)
	}
	// 以 JSON 格式输出查询结果
	c.ServeJSON()
}

// Put ...
// @Title Put
// @Description update the TAchievment
// @Param	id		path 	string	true		"The id you want to update"
// @Param	body		body 	models.TAchievment	true		"body for TAchievment content"
// @Success 200 {object} models.TAchievment
// @Failure 403 :id is not int
// @router /:id [put]
func (c *TAchievmentController) Put() {
	idStr := c.Ctx.Input.Param(":id")
	id, _ := strconv.Atoi(idStr)
	v := models.TAchievment{Id: id}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
		if err := models.UpdateTAchievmentById(&v); err == nil {
			c.Data["json"] = "OK"
		} else {
			c.Data["json"] = err.Error()
		}
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}

// Delete ...
// @Title Delete
// @Description delete the TAchievment
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *TAchievmentController) Delete() {
	idStr := c.Ctx.Input.Param(":id")
	id, _ := strconv.Atoi(idStr)
	if err := models.DeleteTAchievment(id); err == nil {
		c.Data["json"] = utils.Export(1000, "删除成功", id)
	} else {
		c.Data["json"] = utils.Export(1001, "删除失败", err.Error())
	}
	c.ServeJSON()
}
